import React from "react";
import {post, tree} from '@/utils/request';
import {ApiResult} from '@/utils/data';
import {exportWeb} from '@/utils/utils';
import config from '@/config';
import {SysModuleModel} from './data';
import {DndNodeModel} from "@/global";

/** 获取系统模块接口基地址 */
const getUrl = (url: string) => config.apiBaseUrl + '/sys/module' + url;

/** 系统模块接口服务 */
const service = {
  /** 新建系统模块 */
  async insert(data: SysModuleModel): Promise<ApiResult> {
    return post(getUrl('/insert'), data);
  },

  /** 修改系统模块 */
  async update(data: SysModuleModel): Promise<ApiResult> {
    return post(getUrl('/update'), data);
  },

  /** 保存系统模块 */
  async save(created: boolean, oldRecord: any, newRecord: any): Promise<ApiResult> {
    return created ? service.insert(newRecord) : service.update(newRecord)
  },

  /** 修改系统模块状态 */
  async updateStatus(ids: string, status: boolean | number): Promise<ApiResult> {
    return post(getUrl('/updateStatus'), {ids, status});
  },

  /** 修改系统模块展开状态 */
  async updateExpandStatus(ids: string, status: boolean | number): Promise<ApiResult> {
    return post(getUrl('/updateExpandStatus'), {ids, status});
  },

  /** 保存节点拖拽数据 */
  async updateDnd(data: DndNodeModel[]) {
    return post(getUrl('/updateDnd'), data);
  },

  /** 根据主键字符串删除系统模块 */
  async delete(ids: string): Promise<ApiResult> {
    return post(getUrl('/delete'), {ids});
  },

  /** 根据主键查询单个系统模块 */
  async selectById(id: string): Promise<ApiResult<SysModuleModel>> {
    return post(getUrl('/selectById'), {id});
  },

  /** 查询系统模块列表 */
  async selectList(data: any) {
    return post(getUrl('/selectList'), data);
  },

  /** 查询系统模块树形列表 */
  async selectTreeList(data?: any) {
    return tree(getUrl('/selectList'), data, service.convertTreeNode);
  },

  /** 转换Tree节点属性 */
  convertTreeNode(model: SysModuleModel) {
    model.key = model.id;
    model.value = model.id;
    model.title = model.name;
    model.icon = (<i className={model.menu ? model.webCls : 'icon-settings'}/>);
  },

  /** 导出系统模块列表 */
  export(data: any) {
    exportWeb(getUrl('/exportWeb'), data);
  },
}

export default service;
